package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsCategory;
import com.qf.pojo.DtsGoods;
import com.qf.service.DtsAdminBrandService;
import com.qf.service.DtsAdminGoodsService;
import com.qf.service.DtsCategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.GoodsAllinone;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/goods")
public class DtsAdminGoodsController {

    @Autowired
    DtsAdminGoodsService adminGoodsService;

    @Autowired
    DtsAdminBrandService adminBrandService;

    @Autowired
    DtsCategoryService dtsCategoryService;

    @Autowired
    DtsAdminBrandService brandService;

    @PostMapping("/create")
    public Object create(@RequestBody GoodsAllinone goodsAllinone){
        Object data = adminGoodsService.create(goodsAllinone);
        return data;
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsGoods goods){
        Object data = adminGoodsService.delete(goods);
        return data;
    }

    @RequestMapping("/update")
    public Object update(@RequestBody GoodsAllinone goodsAllinone){
        Object data = adminGoodsService.update(goodsAllinone);
        return data;
    }

    @RequestMapping("/catAndBrand")
    public  Object catAndBrand(){
        List<CatVo> categoryList = dtsCategoryService.queryCategoryList();
        List<Map<String,Object>> brandList = brandService.queryBrandList();

        Map<String,Object> data = new HashMap<>();
        data.put("categoryList",categoryList);
        data.put("brandList",brandList);
        return ResponseUtil.ok(data);
    }

    @RequestMapping("/detail")
    public Object detail(Integer id){
        Map<String,Object> data = adminGoodsService.queryDetailById(id);
        return ResponseUtil.ok(data);
    }

    @GetMapping("/list")
    public Object list(@RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                       @RequestParam(value = "limit",required = false,defaultValue = "10")Integer limit,
                       String sort, String order, String goodsSn, String name){

        Subject subject = SecurityUtils.getSubject();
        DtsAdmin admin = (DtsAdmin) subject.getPrincipal();
        List<Integer> brandId = new ArrayList<>();
        if(subject.hasRole("品牌制造商")){
            brandId =  adminBrandService.selectId(admin.getId());
        }

        IPage<DtsGoods> iPage = adminGoodsService.queryPage(page,limit,sort,order,goodsSn,name,brandId);

        Map<String,Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);
    }


}
